from datetime import datetime

import requests
from lxml import etree
import re
import os
import pdfkit


# 参考https://www.52pojie.cn/thread-990598-1-1.html

# 总结： 刚刚开始的时候，图片没有 下载下来，是因为生成的html文件中，图片的url不完整，所以图片就没有下载下来
# 首先，先通过xpath 定位到你想要保存的内容的路径，例如下面的ehtml.xpath('//div[@id="cnblogs_post_body"]')[0]  因为这里是一个list，所以要
# 用下标获取第一个元素，否则python 报错





if __name__ == '__main__':
    # url = 'https://blog.csdn.net/hancoder/article/details/106922139'  # 获取教程url地址
    # folder = '数据在计算机中的存储形式' + datetime.strftime(datetime.now(), '%Y-%m-%d-%H-%M-%S')
    # savepath = 'D:\\csdn\\'  # 保存位置
    #
    # getdata(url, True, folder)  # 后面True表示生成PDF，False不生成
    #
    # print("保存路径： " + savepath + folder + '\\' + folder + '.pdf')
    # os.startfile(savepath + folder )
    path_wk = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'  # wkhtmltopdf安装位置
    config = pdfkit.configuration(wkhtmltopdf=path_wk)
    options = {
        'enable-local-file-access': None,
        'page-size': 'A4',
        'margin-top': '0.75in',
        'margin-right': '0.75in',
        'margin-bottom': '0.75in',
        'margin-left': '0.75in',
        'encoding': "UTF-8",
        'outline': None,
        'footer-right': '[page] of [topage]',  # 添加页码

    }
    mybatis_dict = {}

    mybatis_dict["https://mybatis.org/mybatis-3/zh/getting-started.html"] = "01-mybatis-入门.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/configuration.html"] = "02-mybatis-xml配置.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/sqlmap-xml.html"] = "03-mybatis-xml映射文件.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/dynamic-sql.html"] = "04-mybatis-动态SQL.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/java-api.html"] = "05-mybatis-Java API.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/statement-builders.html"] = "06-mybatis-SQL语句构建器.pdf"
    mybatis_dict["https://mybatis.org/mybatis-3/zh/logging.html"] = "07-mybatis-日志.pdf"
    mybatis_dict["https://baomidou.com/pages/24112f/"] = "01-mybatis-plus-简介.pdf"
    mybatis_dict["https://baomidou.com/pages/226c21/"] = "02-mybatis-plus-快速开始.pdf"
    mybatis_dict["https://baomidou.com/pages/bab2db/"] = "03-mybatis-plus-安装.pdf"
    mybatis_dict["https://baomidou.com/pages/3b5af0/"] = "04-mybatis-plus-配置.pdf"
    mybatis_dict["https://baomidou.com/pages/223848/"] = "05-mybatis-plus-注解.pdf"
    mybatis_dict["https://baomidou.com/pages/b7dae0/"] = "06-mybatis-plus-快速测试.pdf"
    mybatis_dict["https://baomidou.com/pages/779a6e/"] = "07-mybatis-plus-代码生成器.pdf"
    mybatis_dict["https://baomidou.com/pages/49cc81/"] = "08-mybatis-plus-CRUD接口.pdf"
    mybatis_dict["https://baomidou.com/pages/10c804/"] = "08-1-mybatis-plus-条件构造器.pdf"
    mybatis_dict["https://baomidou.com/pages/e131bd/"] = "09-mybatis-plus-主键策略.pdf"
    mybatis_dict["https://baomidou.com/pages/568eb2/"] = "10-mybatis-plus-自定义ID生成器.pdf"
    mybatis_dict["https://baomidou.com/pages/6b03c5/"] = "11-mybatis-plus-逻辑删除.pdf"
    mybatis_dict["https://baomidou.com/pages/8390a4/"] = "12-mybatis-plus-通用枚举.pdf"
    mybatis_dict["https://baomidou.com/pages/fd41d8/"] = "13-mybatis-plus-字段类型处理器.pdf"
    mybatis_dict["https://baomidou.com/pages/4c6bcf/"] = "14-mybatis-plus-自动填充功能.pdf"
    mybatis_dict["https://baomidou.com/pages/42ea4a/"] = "15-mybatis-plus-SQL注入器.pdf"

    # 遍历字典，下载文章
    for key in mybatis_dict:
        pdfkit.from_url(key, 'D:\\csdn\\mysql\\' + mybatis_dict[key],
                        options=options, configuration=config, verbose=True)
